package com.kira.scaffoldmvc;

import com.kira.scaffoldmvc.Disruptor.StudentEvent;
import com.kira.scaffoldmvc.ShutDownHook.CloseStrategy;
import com.kira.scaffoldmvc.ShutDownHook.CloseStrategyContainer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import jakarta.annotation.PreDestroy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
@Slf4j
@MapperScan("com.kira.scaffoldmvc.Mapper")
@EnableConfigurationProperties
@ConfigurationPropertiesScan
@EnableAsync
public class ScaffoldMvcApplication {


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ScaffoldMvcApplication.class, args);

        // 获取应用实例
        ScaffoldMvcApplication application = context.getBean(ScaffoldMvcApplication.class);

//        // 注册 JVM 关闭钩子
//        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
//            log.info("JVM 关闭钩子触发，开始优雅关闭线程池...");
//            application.shutdownAllExecutorServices();
//            log.info("所有线程池已优雅关闭，所有任务执行完成");
//        }));

    }


    //获取所有的关闭策略类
    @Autowired
    CloseStrategyContainer closeStrategyContainer;

    // 同时保留@PreDestroy作为备选关闭方式
    @PreDestroy
    public void onDestroy() {

        // 获取所有"close"类型的关闭策略,然后执行策略类的close()关闭方法
        List<CloseStrategy> strategies = closeStrategyContainer.getBean("close");
        if (strategies != null) {
            for (CloseStrategy strategy : strategies) {
                strategy.close(); // 执行关闭操作
            }
        }

    }

}
